using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

namespace Gameplay.PVE
{
    public class PveMap2D
    {
        private Transform frontRoot;
        private Transform backRoot;

        private Transform front1;
        private Transform front2;

        private Transform back1;
        private Transform back2;

        private GameObject root;
        private Vector3 currentPosition;

        private float frontWidth;
        

        private Camera mainCamera;
        private float lastCameraX;

        public void Init(Vector3 position)
        {
            mainCamera = Camera.main;
            currentPosition = position;
            root = new GameObject("[PveMap2D]");
            root.transform.SetParent(PveScene.pveRootTrans);
            lastCameraX = mainCamera.transform.position.x;
        }
        
        //正交视角
        public void Create2DMap()
        {
            frontRoot = new GameObject("Front").transform;
            frontRoot.SetParent(root.transform);
            backRoot = new GameObject("Back").transform;
            backRoot.SetParent(root.transform);
            float scale = 1.8f;
            frontWidth = 10 * scale;
            
            var front = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 101);
            front.transform.SetParent(frontRoot);
            front.transform.localScale = new Vector3(scale,scale,scale);
            front.transform.position = currentPosition + new Vector3(0,-23,30);
            front1 = front.transform;
            
            front = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 101);
            front.transform.SetParent(frontRoot);
            front.transform.localScale = new Vector3(scale,scale,scale);
            front.transform.position = currentPosition + new Vector3(frontWidth,-23,30);
            front2 = front.transform;
            
            
            var back = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 102);
            back.transform.SetParent(backRoot);
            back.transform.localScale = new Vector3(scale,scale,scale);
            back.transform.position = currentPosition + new Vector3(0,-23,31);
            back1 = back.transform;
            
            back = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 102);
            back.transform.SetParent(backRoot);
            back.transform.localScale = new Vector3(scale,scale,scale);
            back.transform.position = currentPosition + new Vector3(frontWidth,-23,31);
            back2 = back.transform;
        }

        //投影视角
        public void Create2DMapProject()
        {
            frontRoot = new GameObject("Front").transform;
            frontRoot.SetParent(root.transform);
            backRoot = new GameObject("Back").transform;
            backRoot.SetParent(root.transform);
            float scale = 160f;
            frontWidth = 10 * scale;
            
            var front = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 100);
            front.transform.SetParent(frontRoot);
            front.transform.localScale = new Vector3(scale,scale,scale);
            front.transform.localEulerAngles = new Vector3(0,180,0);
            front.transform.position = currentPosition + new Vector3(0,7.5f,0.86f);
            front1 = front.transform;
            
            /*front = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 101);
            front.transform.SetParent(frontRoot);
            front.transform.localScale = new Vector3(scale,scale,scale);
            front.transform.position = currentPosition + new Vector3(frontWidth,-7,5);
            front2 = front.transform;*/
            
            
            /*var back = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 102);
            back.transform.SetParent(backRoot);
            back.transform.localScale = new Vector3(scale,scale,scale);
            back.transform.position = currentPosition + new Vector3(0,-7,6);
            back1 = back.transform;
            
            back = PveResourceManager.GetResource(PveResourceManager.EPveResType.Map, 102);
            back.transform.SetParent(backRoot);
            back.transform.localScale = new Vector3(scale,scale,scale);
            back.transform.position = currentPosition + new Vector3(frontWidth,-7,6);
            back2 = back.transform;*/
        }

        public void Update()
        {
            return;
            //先处理后景
            float currentX = mainCamera.transform.position.x;
            float adjustOffset = (currentX - lastCameraX) / 2;
            var backPos = back1.position;
            backPos += new Vector3(adjustOffset, 0, 0);
            back1.position = backPos;
            backPos = back2.position;
            backPos += new Vector3(adjustOffset, 0, 0);
            back2.position = backPos;

            //前景边缘处理
            Vector2 cameraArea = GetCameraArea();
            if (front1.position.x + frontWidth / 2 < cameraArea.x) //如果左边地图的右边缘再镜头的左边了
            {
                var position = front1.position;
                position = new Vector3(front2.position.x + frontWidth,position.y,position.z);
                front1.position = position;
                var temp = front2;
                front2 = front1;
                front1 = temp;
            }
            else if (front1.position.x - frontWidth / 2 >= cameraArea.x)//左边超过了
            {
                var position = front1.position;
                position = new Vector3(position.x - frontWidth,position.y,position.z);
                front2.position = position;
                var temp = front2;
                front2 = front1;
                front1 = temp;
            }
            
            //后景边缘处理
            if (back1.position.x + frontWidth / 2 < cameraArea.x) //如果左边地图的右边缘再镜头的左边了
            {
                var position = back1.position;
                position = new Vector3(back2.position.x + frontWidth,position.y,position.z);
                back1.position = position;
                var temp = back2;
                back2 = back1;
                back1 = temp;
            }
            else if (back1.position.x - frontWidth / 2 >= cameraArea.x)//左边超过了
            {
                var position = back1.position;
                position = new Vector3(position.x - frontWidth,position.y,position.z);
                back2.position = position;
                var temp = back2;
                back2 = back1;
                back1 = temp;
            }

            lastCameraX = mainCamera.transform.position.x;
        }

        private Vector2 GetCameraArea()
        {
            float x = mainCamera.transform.position.x;
            float halfWidth = mainCamera.orthographicSize / Screen.height * Screen.width;
            return new Vector2(x - halfWidth, x + halfWidth);
        }
        
        public void LoadNextMap()
        {
            
        }
    }
}